<?php

namespace App\Models;


class BackLogData extends BaseModel
{
    protected $table = "back_log_data";

    protected $redisTable = "back_log_data";

    protected $rules = [
        'log_id' => 'required',
        'bucket_category_id' => 'required',
        'num' => 'required',
    ];

    protected $message = [
        'log_id.required' => '非法操作',
        'bucket_category_id.required' => '非法操作',
        'num.required' => '非法操作',
    ];

    public function worker()
    {
        return $this->hasOne('App\Models\Worker', 'id', 'worker_id');
    }

    public function member()
    {
        return $this->hasOne('App\Models\Member', 'id', 'buyer_id');
    }

    public function getLogList($shop_id,$bucket_category_id)
    {
        $keyword = request('keyword');
        $where['shop_id'] = $shop_id;
        $where['bucket_category_id'] = $bucket_category_id;
        if ($keyword) {
            if (is_numeric($keyword)) {
                $where['buyer_id'] = $keyword;
            }
        }
        $rows = $this->_getJoinTwoPageList('member', 'worker', $where, "*", ['id'=>'desc'], 15,10, 'id,name', [],'id,name,code');
        return $rows;
    }

    public function addOrUpdate($log_id,$bucket_category_id,$num)
    {
        $data['log_id'] = intval($log_id);
        $data['bucket_category_id'] = intval($bucket_category_id);
        $data['num'] = intval($num);
        if (!$this->_validate($data)) {
            return false;
        }
        $res = $this->_save($data);
        if (!$res) {
            $this->error = '保存失败!';
            return false;
        }
        return $res;
    }

}